/**
 * 智慧小区管理系统 - 店铺租金管理API接口
 * @author Wu.Liang
 * @date 2025-01-30
 * @version 1.0.0
 */

import api from '@/api/request'
import type { ApiResponse, PageResponse } from '@/types/api'

// 租金记录查询参数
export interface ShopRentQueryParams {
  current?: number
  size?: number
  shopId?: number
  ownerId?: number
  paymentStatus?: number
  rentPeriod?: string
  rentMonth?: string
  startDate?: string
  endDate?: string
}

// 租金记录表单数据
export interface ShopRentFormData {
  id?: number
  shopId: number
  ownerId: number
  rentAmount: number
  rentPeriod: string
  rentStartDate: string
  rentEndDate: string
  paymentStatus?: number
  paymentMethod?: string
  invoiceNumber?: string
  remark?: string
}

// 租金记录详情
export interface ShopRentRecordDetail {
  id: number
  shopId: number
  shopName: string
  shopCode: string
  ownerId: number
  ownerName: string
  ownerPhone: string
  rentAmount: number
  rentPeriod: string
  rentStartDate: string
  rentEndDate: string
  paymentStatus: number
  paymentStatusName: string
  paymentDate?: string
  paymentMethod?: string
  paymentMethodName?: string
  invoiceNumber?: string
  remark?: string
  createTime: string
  updateTime: string
}

// 支付信息
export interface PaymentInfo {
  paymentAmount: number
  paymentMethod: string
  paymentDate?: string
  invoiceNumber?: string
  paymentRemark?: string
  paymentVoucher?: string
}

// 租金统计信息
export interface ShopRentStatistics {
  totalRecords: number
  totalRentAmount: number
  paidRecords: number
  paidAmount: number
  unpaidRecords: number
  unpaidAmount: number
  overdueRecords: number
  overdueAmount: number
  paymentRate: number
  averageRentAmount: number
  startDate?: string
  endDate?: string
  shopId?: number
  shopName?: string
}

// 租金记录分页响应
export interface ShopRentPageResponse extends PageResponse {
  records: ShopRentRecordDetail[]
}

/**
 * 店铺租金管理API
 */
export const shopRentApi = {
  /**
   * 分页查询租金记录
   * @param params 查询参数
   * @returns 分页结果
   */
  getRentRecordPage: (params: ShopRentQueryParams) => {
    return api.get<ApiResponse<ShopRentPageResponse>>('/api/region/shop-rents/page', { params })
  },

  /**
   * 根据ID获取租金记录详情
   * @param id 记录ID
   * @returns 租金记录详情
   */
  getRentRecordDetail: (id: number) => {
    return api.get<ApiResponse<ShopRentRecordDetail>>(`/api/region/shop-rents/${id}`)
  },

  /**
   * 创建租金记录
   * @param data 租金记录信息
   * @returns 操作结果
   */
  createRentRecord: (data: ShopRentFormData) => {
    return api.post<ApiResponse<number>>('/api/region/shop-rents', data)
  },

  /**
   * 更新租金记录
   * @param id 记录ID
   * @param data 租金记录信息
   * @returns 操作结果
   */
  updateRentRecord: (id: number, data: ShopRentFormData) => {
    return api.put<ApiResponse<boolean>>(`/api/region/shop-rents/${id}`, data)
  },

  /**
   * 删除租金记录
   * @param id 记录ID
   * @returns 操作结果
   */
  deleteRentRecord: (id: number) => {
    return api.delete<ApiResponse<boolean>>(`/api/region/shop-rents/${id}`)
  },

  /**
   * 支付租金
   * @param recordId 记录ID
   * @param paymentInfo 支付信息
   * @returns 操作结果
   */
  payRent: (recordId: number, paymentInfo: PaymentInfo) => {
    return api.post<ApiResponse<boolean>>(`/api/region/shop-rents/${recordId}/pay`, paymentInfo)
  },

  /**
   * 获取租金统计
   * @param params 统计参数
   * @returns 统计信息
   */
  getRentStatistics: (params: {
    shopId?: number
    startDate?: string
    endDate?: string
  }) => {
    return api.get<ApiResponse<ShopRentStatistics>>('/api/region/shop-rents/statistics', { params })
  },

  /**
   * 根据店铺ID获取租金记录列表
   * @param shopId 店铺ID
   * @returns 租金记录列表
   */
  getRentRecordsByShopId: (shopId: number) => {
    return api.get<ApiResponse<ShopRentRecordDetail[]>>(`/api/region/shop-rents/shop/${shopId}`)
  },

  /**
   * 根据业主ID获取租金记录列表
   * @param ownerId 业主ID
   * @returns 租金记录列表
   */
  getRentRecordsByOwnerId: (ownerId: number) => {
    return api.get<ApiResponse<ShopRentRecordDetail[]>>(`/api/region/shop-rents/owner/${ownerId}`)
  },

  /**
   * 根据支付状态获取租金记录列表
   * @param paymentStatus 支付状态
   * @returns 租金记录列表
   */
  getRentRecordsByPaymentStatus: (paymentStatus: number) => {
    return api.get<ApiResponse<ShopRentRecordDetail[]>>(`/api/region/shop-rents/status/${paymentStatus}`)
  }
}
